package com.sendpo.zjx.sendpo.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.table.MerchandiseCollection;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 朱俊鑫 on 2018
 */

public class MyselfCollectionListActivity extends AppCompatActivity {

    LinearLayoutManager lm;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myselfcollection);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }
    private void initComponent() {
        lm=new LinearLayoutManager(this, OrientationHelper.VERTICAL,false);
        recyclerview.setLayoutManager(lm);
        List<MerchandiseCollection>  collections=Xutils.queryAll(getApplication(), MerchandiseCollection.class);
        if(collections==null||collections.size()==0){
            return;
        }
        recyclerview.setAdapter(new MyRecylcerAdaper(this,collections));
    }
    @OnClick(value = {R.id.backbt})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.backbt:
                finish();
                break;
        }
    }

    public class MyRecylcerAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        public LayoutInflater inflater;
        public List<MerchandiseCollection> merchandiseCollections;
       public MyRecylcerAdaper(Context context,List<MerchandiseCollection> databean){
            inflater=LayoutInflater.from(context);
            this.merchandiseCollections=databean;
       }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new IndexHoler(this.inflater.inflate(R.layout.layout_collection_listitem, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
           IndexHoler ih= (IndexHoler) holder;
            final MerchandiseCollection data = (MerchandiseCollection)this.merchandiseCollections.get(position);
            ih.rootView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("merchandiseId",data.getMerchandiseId());
                    Tools.redirectActivity(MyselfCollectionListActivity.this, GeneicStoreInfoActivity.class, map);
                }
            });
            Glide.with(MyselfCollectionListActivity.this)
                    .load(data.getMerchandiseHeadimg())
                    .asBitmap()
                    .override(300,300)
                    .placeholder(R.color.default_lightgray)
                    .into(ih.iv);
            ih.name.setText("" + data.getMerchandiseName());
            ih.originprice_tv.setText("¥" + data.getMerchandiseOriginalprice());
            ih.price_tv.setText("¥" + data.getMerchandiseCurrentprice());
            ih.text.setText("" + data.getMerchandiseDetails());
            ih.remove_bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DbManager dbManager = Tools.getDbManager(getApplication(), Tools.DBNAME, Tools.DBVERSION);
                    try {
                        MerchandiseCollection cb = dbManager.selector(MerchandiseCollection.class).where("merchandiseId", "=", data.getMerchandiseId())
                                .findFirst();
                        dbManager.delete(cb);
                        delete(position);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }

                }
            });
        }
         public void delete(int position){
             merchandiseCollections.remove(position);
              notifyDataSetChanged();
         }
        @Override
        public int getItemCount() {
            return merchandiseCollections.size();
        }
        public class IndexHoler extends RecyclerView.ViewHolder {
            public ImageView iv;
            public TextView name;
            public TextView originprice_tv;
            public TextView price_tv;
            public View rootView;
            public TextView text;
             public Button remove_bt;
            public IndexHoler(View paramView) {
                super(paramView);
                remove_bt=itemView.findViewById(R.id.remove_bt);
                this.originprice_tv = ((TextView)paramView.findViewById(R.id.originprice_tv));
                originprice_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
                originprice_tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线
                originprice_tv.getPaint().setAntiAlias(true);// 抗锯齿
                originprice_tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
                this.name = ((TextView)paramView.findViewById(R.id.name));
                this.text = ((TextView)paramView.findViewById(R.id.text));
                this.price_tv = ((TextView)paramView.findViewById(R.id.price_tv));
                this.rootView = paramView;
                this.iv = ((ImageView)paramView.findViewById(R.id.iv));
            }
        }
    }


}
